/**
 * 
 */
var app = angular.module('idcApp', []);

var init = $(function() {
	$('#if-0').on('load', function(){
		iFrameHeight('if-0');
	});

	//$('#if-0').hide();
	$('#if-0').prop('src','../../mainpage/main.jsp');//首页地址

});


// 初始化导航栏
var initController = app
		.controller(
				"indexController",
				function($scope, $http) {
					$http({
						url : '/loadMenu',
						method : 'GET'
					}).success(function(data, header, config, status) {
						// 响应成功
						$scope.menus = data;
					}).error(function(data, header, config, status) {
						// 处理响应失败
					});

					$scope.openTab = function(subMenu) {
					var tabTemplate = "<li id=\"tab-"
							+ tabCounter
							+ "\"><a href='#' data-toggle='tab' onclick='activeTab(this);'><i class='glyphicon glyphicon-film' ></i>&nbsp;"
							+ subMenu.name
							+ "</a><i onclick=\"closeTab(this);\" class='close-tab glyphicon glyphicon-remove'></i></li>";
					tabBar.append(tabTemplate);
					
					
					//增加iframe页
					var ifTemplate = "<iframe id=\"if-"+tabCounter+"\" name=\"if-"+tabCounter+"\" ></iframe>";
					if(curTabIdx == 0 ){
						$("#main-iframe").append(ifTemplate);//增加iframe页面
						setIfAddress('if-'+tabCounter,subMenu.url);//自动调整高度
						$('#if-0').hide();//隐藏首页				
						$('#tab-'+tabCounter).prop('class','active');
						$('#tab-0').prop('class','');
						curTabIdx = tabCounter;
					}else{
						$("#main-iframe").append(ifTemplate);//增加iframe页面
						setIfAddress('if-'+tabCounter,subMenu.url);//自动调整高度
			
						$('#if-'+curTabIdx).hide();//隐藏首页
						$('#tab-'+tabCounter).prop('class','active');
						$('#tab-'+curTabIdx).prop('class','');
						curTabIdx = tabCounter;
					}
					tabCounter++;
				}

				});

// 主面板内容切换
var tabCounter = 1;
var curTabIdx =  0;
var tabBar = $('#tab-Content');

var closeTab = function(obj) {	
	
	 var liObj = $( obj ).closest( "li" );
	 var tabId = liObj.prop("id");
	 
	 liObj.remove().prop( "aria-controls" );
	 $( "#" + tabId ).remove();	 
	 var idx = tabId.split('-')[1]; 
     $('#if-'+idx).remove();
      
     
     //设置首页
	curTabIdx = 0;
	$('#tab-0').prop('class','active');
	$('#tab-'+idx).prop('class','');
    $('#if-0').show();

};

var activeTab = function(obj){
	var tabId = $( obj ).closest( "li" ).prop( "id" );
	var idx = tabId.split('-')[1];
	
	if(curTabIdx != idx){
		//处理tab
		$('#tab-'+idx).prop('class','active');
		$('#tab-'+curTabIdx).prop('class','');
		//处理if
		$('#if-'+curTabIdx).hide();//隐藏首页
		$('#if-'+idx).show();//隐藏首页
		
		curTabIdx = idx;
	}
};

//给每个iframe页增加注册事件，提高加载成功率
var setIfAddress = function(ifId,url){
	//alert(ifId+" "+url);
	$('#'+ifId).on('load',function(){
		//$('#'+ifId).fadeIn('slow');
		iFrameHeight(ifId);
	});
	//$('#'+ifId).hide();
	$('#'+ifId).prop('src',url);//首页地址
};

function iFrameHeight(ifId) {
	//alert(ifId);
	var ifm = document.getElementById(ifId);
	var subWeb = document.frames ? document.frames[ifId].document
			: ifm.contentDocument;
	if (ifm != null && subWeb != null) {
		ifm.height = subWeb.body.scrollHeight;
		if (ifm.height < 700) {
			ifm.height = 700;
		}
		//$('#if-0').fadeIn('slow');
	}
};

function logout(){
	
	 $.ajax({
   	  url:"/login/logout",
   	 async:false,
   	  success:function(){
   		  
   		  self.location = '/';
   	  }
     });
}

